package org.geektime.task.sboauth.controller;

import org.geektime.task.sboauth.config.OAuthConfig;
import org.geektime.task.sboauth.config.OAuthSourceEnum;
import org.geektime.task.sboauth.model.OAuthCallback;
import org.geektime.task.sboauth.model.OAuthUser;
import org.geektime.task.sboauth.request.AbstractOAuthRequest;
import org.geektime.task.sboauth.request.GiteeOAuthRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p> GiteeOAuthController.java.</p>
 * <p>Description:  .</p>
 * <p>Company: .</p>
 * <p>@author Long Yu</p>
 * <p>@version .</p>
 * 2021年04月21日 22:02:49
 */
@Controller
@RequestMapping("/oauth/gitee")
public class GiteeOAuthController {

    @Autowired
    private OAuthConfig oAuthConfig;


    /**
     * 跳转到Gitee的授权页面
     * @return
     */
    @RequestMapping("/authorize")
    public void authorize(HttpServletResponse response) throws Exception {

        String defaultUrl = OAuthSourceEnum.GITEE.authorize();

        String authorizeUrl = defaultUrl + "?client_id="+oAuthConfig.getClientId()+"&redirect_uri="+oAuthConfig.getRedirectUri()+"&response_type=code";

        response.sendRedirect(authorizeUrl);

    }


    /**
     * 为Gitee提供的回调接口
     * @return
     */
    @RequestMapping("/giteecallback")
    public String giteecallback(HttpServletRequest request,OAuthCallback callback){
        AbstractOAuthRequest authRequest = new GiteeOAuthRequest(oAuthConfig);
        OAuthUser user = authRequest.login(callback);

        request.getSession().setAttribute("user",user);

        return "redirect:/";
    }











}
